<?php
class Member_account_logAction extends CommonAction {
	
	function index(){
        $myid=D('User')->where(' id = '.session(C('USER_AUTH_KEY')))->getField('member_id');
        $this->myLevel=D('Member')->where('id = '.$myid)->getfield('level');
        if($_GET['member_id']){
            $member_id = $this->_get('member_id');
        }else{
            $member_id = $myid;
        }

        if($this->myLevel == 1){
            if(isset($_GET['stype'])&&$_GET['stype']==1){
                $this->stype=1;//推荐收益
            }else{
                $this->stype=0;//营业收益
            }
            $divideDB = D('Member_account_log');
            $map['i.member_id']=$member_id;
            if($this->stype == 1){
                $map['i.types']=1;
            }else{
                $map['i.pdstate']=4;
            }
            $pd_time = $this->_get('pd_time');
            if ($pd_time == 1) {
                $find_time = $this->_get('find_time');
                $month_first = date("Y-m-01", strtotime($find_time)); //当月的第一天
                $month_last = date("Y-m-d", strtotime("$month_first +1 month -1 day"));
                $month_first = strtotime($month_first);
                $month_last = strtotime($month_last);
                $map['i.dateline'] = array(array('gt', $month_first), array('lt', $month_last));
            }
            if($_GET['pagesize']){
                $pagesize=$_GET['pagesize'];
            }else{
                $pagesize=10;
            }
            if($_GET['keywords']){
                $map['o.ordernumber']=array('like','%'.$_GET['keywords'].'%');
            }
            if($_GET['time']){
                $start=strtotime(date('Y-m-d',strtotime($_GET['time'])));
                $end=strtotime(date('Y-m-d',strtotime($_GET['time'])))+86400;
                $map['dateline']=array('between',array($start,$end));
            }
            $this->initView();
            $divideDB->_list2($this->view, $map, 'dateline desc', $pagesize);
        }else{
            $this->listmap = ' m.member_id = '.$member_id;
            //$this->listorder="dateline desc";
            $this->member_id = $member_id;
            $this->rename='m';
            $this->join='__ORDER__ o on o.id = m.order_id';
            $this->field='m.*,o.ordernumber as ordernumber,o.total as total';
            $this->_list($this->rename,$this->join,$this->field);
        }
		$this->display();
	}
	   public function draw_list(){
		$myid=D('User')->where(' id = '.session(C('USER_AUTH_KEY')))->getField('member_id');
		
        $bank=M('Member_bank')->where('member_id = '. $myid)->select();
		   if($myid>0){
            $this->listmap='member_id = '.$myid.' and  hbstatus = 1 ';
            if($this->_get('keywords')){
               $this->listmap .=' and id = '.$this->_get('keywords');
            }
            if(isset($_GET['level'])){
            }
             $this->initView();
			 $this->pagesize=5;
             parent::_list($this->view,'','');
				
        }
		$this->assign('bank',$bank);
		$this->display();
	}
	
	
	public function add(){
		$this->member_id = $this->_get('member_id');
		parent::add();
	}
	public function insert(){
		
		$money = $this->_post('money');
		$map['id'] = $this->_post('member_id');
		$memberInfo=M('Member')->where($map)->find();
		$chanageType = $this->_post('type');
		$memberDb = D('Member');
		if($chanageType == 'setInc'){
			$memberDb -> where($map) -> setInc('money', $money);
			$data['total']=$memberInfo['money']+$money;
		}else{
			$_POST['money'] = $_POST['money'] * -1;
			$memberDb -> where($map) -> setDec('money', $money);
			$data['total']=$memberInfo['money']-$money;
		}

		$data['user_id'] = session(C('USER_AUTH_KEY') );
		$data['member_id'] = $this->_post('member_id');
		$data['remark'] = $_POST['remark'];
		
		$data['changenum'] = $_POST['money'];
		$data['dateline'] = time();
		
		$data['site_id'] = $this->siteid;
		
		
		//$cret = $this -> dao -> create($data);
		
		/*if(!$cret){
			$this->error($this->dao -> getError());
		}*/
		
		$ret = $this -> dao -> data($data) -> add();
		/*if($ret){
			//微信通知
			$wxuser = $memberDb -> where(array('id' => $this->_post('member_id'))) -> find();
			if($_POST['money'] > 0){
				$text = "尊敬的".$wxuser['nickname'].":\r\n您的账户资金增加".abs($_POST['money'])."\r\n原因：".$data['remark']."\r\n目前余额为：".$data['total'];
			}else{
				$text = "尊敬的".$wxuser['nickname'].":\r\n您的账户资金减少".abs($_POST['money'])."\r\n原因：".$data['remark']."\r\n目前余额为：".$data['total'];
			}
			sendWXMsg($wxuser['openid'], $text);
			
			$this->success('操作成功！', U('index', array('member_id' => $this->_post('member_id'))));
		}else{
			$this->error($this->dao -> getError());
		}
		*/
	}
	


}
